package com.webridge.plm.test;


import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;


import wt.conflict.ConflictResolution;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTHashSet;
import wt.fc.collections.WTSet;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.session.SessionServerHelper;
import wt.util.WTException;
import wt.vc.VersionControlConflictType;
import wt.vc.VersionControlHelper;
import wt.vc.VersionControlResolutionType;

public class Delete implements RemoteAccess {
	
    public static void main(String[] args) throws Exception {
        RemoteMethodServer.getDefault().setUserName("wcadmin");
        RemoteMethodServer.getDefault().setPassword("wcadmin");

        remoteTest(args);
    }

    public static void remoteTest(String[] args) throws Exception {
        if (RemoteMethodServer.ServerFlag) {
        	WTPart target = getPartByNumber(args[0], false);
        	ConflictResolution[] resolutions = { new ConflictResolution(VersionControlConflictType.LATEST_ITERATION_DELETE, VersionControlResolutionType.ALLOW_LATEST_ITERATION_DELETE)};
        	WTSet toDelete = new WTHashSet();
        	toDelete.add(target);
        	VersionControlHelper.service.deleteIterations(toDelete, resolutions);

        } else {
            RemoteMethodServer.getDefault().invoke("remoteTest", Delete.class.getName(), null, new Class[] { String[].class },
                    new Object[] { args });
        }
    }
    public static WTPart getPartByNumber(String number, boolean accessControlled) throws WTException {
        WTPart part = null;
        try {
            number = number.toUpperCase();
            if (!RemoteMethodServer.ServerFlag) {
                return (WTPart) RemoteMethodServer.getDefault().invoke("getPartByNumber", Delete.class.getName(),
                        null, new Class[] { String.class, boolean.class }, new Object[] { number, accessControlled });
            } else {
                boolean enforce = SessionServerHelper.manager.setAccessEnforced(accessControlled);
                try {
                    WTPartMaster partMaster = null;
                    QuerySpec querySpec = new QuerySpec(WTPartMaster.class);
                    SearchCondition searchCondition = new SearchCondition(WTPartMaster.class, WTPartMaster.NUMBER,
                            SearchCondition.EQUAL, number, false);
                    querySpec.appendSearchCondition(searchCondition);
                    QueryResult queryResult = PersistenceHelper.manager.find(querySpec);
                    while (queryResult.hasMoreElements()) {
                        partMaster = (WTPartMaster) queryResult.nextElement();
                        QueryResult queryResult2 = VersionControlHelper.service.allVersionsOf(partMaster);
                        if (queryResult2.hasMoreElements()) {
                            part = (WTPart) queryResult2.nextElement();
                        }
                    }
                } finally {
                    SessionServerHelper.manager.setAccessEnforced(enforce);
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        
        return part;
    }
}
